/*
* introduction: This code is just to interpret s-expression.
* thanks-to https://github.com/maryrosecook/littlelisp
*
* author ufo
* email ufo5260987423@163.com
* date 28 March 2014 
*
* quality Firefox 28.0 +ubuntu 12.04 accept
* relyOn library.js
*	 sytexParse.js
*	 keyWords.js
*/
var environment=new Context({},new Context(library));
function Context(scope, parent){
	this.scope = scope;
	this.parent = parent;
	this.get = function(identifier){
		if (identifier in this.scope)
			return this.scope[identifier];
		else if (this.parent != undefined)
			return this.parent.get(identifier);
		};
	this.set = function (identifier, value) {
		this.scope[identifier] = value;
	};
}
function interpret(input, context){//This is the identifier processor,but do not process inner-identifier.
	if (context == undefined)
		return interpret(input ,environment)
	else
		if(input instanceof Array)
			return interpretList(input, context);
		else
			if(input.type == "identifier")
				return context.get(input.value);
			else
				return input.value;
}
function interpretList (input, context){//context started with the value which is undefined.input is the ST.This is the lisp interpreter.
	if (input.length > 0 && input[0].value in keyWords)
		return keyWords[input[0].value](input, context);
	else{
		var list = input.map(function(x)
			{return interpret(x, context);});
		if (list[0] instanceof Function)
			return list[0].apply(undefined, list.slice(1));
		else
			return list;
	}
}
